//
//  MyMcqView.m
//  JHStore
//
//  Created by mac2 on 15/10/13.
//  Copyright (c) 2015年 kiefer. All rights reserved.
//

#import "MyMcqView.h"
#import "EBUtility.h"


@implementation MyMcqView

{
    
    UIView *bgView;
    NSInteger _selectedIndex;
    NSMutableArray *bigBtnArray;
    NSMutableArray *titleaaaArray;
    
}
- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        
        self.frame = CGRectMake(20, (SCREEN_HEIGHT-64)/2-(50+100)/2, SCREEN_WIDTH-40, 50+100);
        self.backgroundColor = RGBA_COLOR(246, 246, 246, 1);
        self.layer.cornerRadius = 8;
        self.layer.masksToBounds = YES;

        _selectedIndex = 10;
  
        _lab = [[UILabel alloc] initWithFrame:CGRectMake(10, 10, SCREEN_WIDTH-40, 30)];
        _lab.textAlignment = NSTextAlignmentLeft;
        _lab.textColor = [EBUtility colorWithHexString:@"#333333" alpha:1];
        _lab.font = [UIFont systemFontOfSize:14];
      
        [self addSubview:_lab];
        
        
        UITextField *textField = [EBUtility textFieldfrome:CGRectMake(10, 50, self.frame.size.width - 20, 50) andText:@"" andColor:nil andView:self];
         textField.borderStyle = UITextBorderStyleRoundedRect;
        textField.delegate =self;
//        if (titleArray.count == 0)
//        {
//            UILabel *gryLabel3 = [[UILabel alloc] initWithFrame:CGRectMake(self.frame.size.width/2-0.5,self.frame.size.height-50, 1, 50)];
//            
//            gryLabel3.backgroundColor = [EBUtility colorWithHexString:@"dfdfdf" alpha:1];
//            [self addSubview:gryLabel3];
//
//            UILabel *gryLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, self.frame.size.height-50, self.frame.size.width, 0.5)];
//            gryLabel.backgroundColor = [EBUtility colorWithHexString:@"dfdfdf" alpha:1];
//            [self addSubview:gryLabel];
//            
//        }
           //cancelbtn;
           //confirmbtn;
        _cancelbtn = [UIButton buttonWithType:UIButtonTypeCustom];
        _cancelbtn.frame = CGRectMake(0+self.frame.size.width/2*0, self.frame.size.height-50, self.frame.size.width/2, 50);
 
        [_cancelbtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        [_cancelbtn setTitle:@"取消" forState:UIControlStateNormal];
            
        [_cancelbtn addTarget:self action:@selector(cancelbtnClick:) forControlEvents:UIControlEventTouchUpInside];
        _cancelbtn.tag = 0;
        [self addSubview:_cancelbtn];

            
        _confirmbtn = [UIButton buttonWithType:UIButtonTypeCustom];
        _confirmbtn.frame = CGRectMake(0+self.frame.size.width/2*1, self.frame.size.height-50, self.frame.size.width/2, 50);
            
        [_confirmbtn setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
        [_confirmbtn setTitle:@"确定" forState:UIControlStateNormal];
            
        [_confirmbtn addTarget:self action:@selector(confirmbtnClick:) forControlEvents:UIControlEventTouchUpInside];
        _confirmbtn.tag = 1;
        [self addSubview:_confirmbtn];
            
        }
    return self;
}

-(void)cancelBtnClick
{
    [bgView removeFromSuperview];
    [self removeFromSuperview];
}

- (void)show
{
    UIWindow *window = [UIApplication sharedApplication].keyWindow;
    self.center = window.center;
    bgView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT)];
    bgView.backgroundColor = [UIColor blackColor];
    bgView.alpha = 0.5;
    
    [window addSubview:bgView];
    [window addSubview:self];
    
    
    UITapGestureRecognizer *recognizerTap = [[UITapGestureRecognizer alloc] initWithTarget:self
                                                                                    action:@selector(handleTapBehind:)];
    
    [recognizerTap setNumberOfTapsRequired:1];
    recognizerTap.cancelsTouchesInView = NO;
    [bgView addGestureRecognizer:recognizerTap];
}

- (void)handleTapBehind:(UITapGestureRecognizer *)sender{
    if (sender.state == UIGestureRecognizerStateEnded){
        CGPoint location = [sender locationInView:nil];
        
        if (![self pointInside:[self convertPoint:location fromView:self.window] withEvent:nil]){
            [bgView removeFromSuperview];
            [self removeFromSuperview];
            //[self dismissWithClickedButtonIndex:0 animated:YES];
        }
    }
}

//h
- (void)abnormalBtnClick:(UIButton *)btn
{
    for (UIButton *selectedBtn in bigBtnArray)
    {
        selectedBtn.selected = NO;
    }
    
    //把当前点击按钮的状态设置成YES
    btn.selected = YES;
    // btn.selected = !btn.selected;
    _selectedIndex = btn.tag;
    NSLog(@" 点击了+++%ld",(long)btn.tag);
}
- (BOOL)textFieldShouldReturn:(UITextField *)textField

{
    
   [textField resignFirstResponder];

    return YES;
    
}
//取消
-(void)cancelbtnClick:(UIButton *)btn
{

    NSLog(@"取消haha--btn.tag-%d",btn.tag);
    [bgView removeFromSuperview];
    [self removeFromSuperview];
}

//确认
-(void)confirmbtnClick:(UIButton *)btn
{
    NSLog(@"确认haha--btn.tag-%d",btn.tag);
  
    
   

    self.clickedBtnIndexBlock(_selectedIndex);
    [bgView removeFromSuperview];
    [self removeFromSuperview];
}


@end
